#pragma once
#include "Common.h"
#include "ObjectPoll.h"

//单例模式
class PageCach {
public:
    static PageCach* GetInstance() {
        return &sInst_;
    }
    Span* MapObjectToSpan(void* obj);
    void ReleaseSpanToPageCach(Span* span);
    //获取一个k页的span
    Span* NewSpan(size_t k);
    void  Lock() { this->pageMtx_.lock(); }
    void  UnLock() { this->pageMtx_.unlock(); }
private:
    SpanList spanList_[NPAGES]; //每个位置是一个spanlist, 按spanlist的页数量映射到哈希桶位置 
    std::mutex pageMtx_;//锁
    std::unordered_map<PAGE_ID, Span*> idSpanMap_;//页号 与 Span节点的映射
    ObjectPoll<Span> spanPoll;//定长对象池
    static PageCach sInst_;//单例对象

    //单例模式防止外部创建对象
    PageCach() {};
    PageCach(const PageCach&) = delete;
    PageCach& operator=(const PageCach&) = delete;
};